using System.Drawing;
using System;
using Lichtspiel.Display;

namespace Lichtspiel.Math
{
    public class MathHelper
    {
        public const double TwoPi = System.Math.PI * 2;

        public static double Clamp(double value, double min, double max) {
            value = (value > max) ? max : value;
            value = (value < min) ? min : value;
            return value;
        }

        public static float Clamp(float value, float min, float max) {
            value = (value > max) ? max : value;
            value = (value < min) ? min : value;
            return value;
        }

        public static int Clamp(int value, int min, int max) {
            value = (value > max) ? max : value;
            value = (value < min) ? min : value;
            return value;
        }

        public static double ToDegrees(double radians) {
            // Factor = 180 / pi
            return radians * 57.295779513082320876798154814105;
        }

        public static float ToDegrees(float radians) {
            return (float)ToDegrees((double)radians);
        }

        public static double ToRadians(double degrees) {
            // Factor = pi / 180
            return degrees * 0.017453292519943295769236907684886;
        }

        public static float ToRadians(float degrees) {
            return (float) ToRadians((double) degrees);
        }

        public static Vector2 AnchorToVector(AnchorLocation anchor, SizeF size) {
            if (anchor == AnchorLocation.TopLeft) {
                return new Vector2(0, 0);
            }

            if (anchor == AnchorLocation.Top) {
                return new Vector2(size.Width / 2f, 0);
            }

            if (anchor == AnchorLocation.TopRight) {
                return new Vector2(size.Width, 0);
            }

            if (anchor == AnchorLocation.Right) {
                return new Vector2(size.Width, size.Height / 2f);
            }

            if (anchor == AnchorLocation.BottomRight) {
                return new Vector2(size.Width, size.Height);
            }

            if (anchor == AnchorLocation.Bottom) {
                return new Vector2(size.Width / 2f, size.Height);
            }

            if (anchor == AnchorLocation.BottomLeft) {
                return new Vector2(0, size.Height);
            }

            if (anchor == AnchorLocation.Left) {
                return new Vector2(0, size.Height / 2f);
            }

            if (anchor == AnchorLocation.Center) {
                return new Vector2(size.Width / 2f, size.Height / 2f);
            }

            return new Vector2(0, 0);
        }
    }
}